/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.system.application.impl;

import java.util.List;

import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.foreveross.bsl.common.utils.mapper.BeanMapper;
import com.foreveross.bsl.mongodb.domain.repository.EntityRepository;
import com.foreveross.bsl.system.application.CloudFunctionService;
import com.foreveross.bsl.system.application.SystemModuleException;
import com.foreveross.bsl.system.application.vo.CloudFunctionVo;
import com.foreveross.bsl.system.model.domain.AppCloudMappingEntry;
import com.foreveross.bsl.system.model.domain.CloudFunction;

/**
 * @description
 * 
 * @author Wangyi
 * @version v1.0
 * 
 * @2013-7-5
 * 
 */
@Named("cloudFunctionService")
public class CloudFunctionServiceImpl implements CloudFunctionService {

	// @Inject
	// private CloudFunctionRepository cloudRepo;
	//
	// @Inject
	// private AppCloudMappingEntryRepository appCloudEntryRepo;

	private final EntityRepository getAppCloudMappingRepository() {
		return new AppCloudMappingEntry().getEntityRepository();
	}

	private EntityRepository getCloudFunctionRepository() {
		return new CloudFunction().getEntityRepository();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.cloudfunc.CloudFunctionService#
	 * add(com.foreveross.bsl.system.application.cloudfunc.vo.CloudFunctionVo)
	 */
	@Override
	public void add(CloudFunctionVo cloudFunc) {
		CloudFunction newEntity = BeanMapper
				.map(cloudFunc, CloudFunction.class);
		// TODO validate
		getCloudFunctionRepository().save(newEntity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.cloudfunc.CloudFunctionService#
	 * update
	 * (com.foreveross.bsl.system.application.cloudfunc.vo.CloudFunctionVo)
	 */
	@Override
	public void update(CloudFunctionVo cloudFunc) {
		if (StringUtils.isEmpty(cloudFunc.getId())) {
			throw new IllegalArgumentException(
					"the id of the passed param can not be empty");
		}
		CloudFunction entity = (CloudFunction) getCloudFunctionRepository().findOne(
				cloudFunc.getId());
		if (null == entity) {
			throw new SystemModuleException("the cloudfunction object of id:"
					+ cloudFunc.getId() + " is not found");
		}
		BeanMapper.copy(cloudFunc, entity);
		// TODO validate
		getCloudFunctionRepository().save(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.cloudfunc.CloudFunctionService#
	 * delete(java.lang.String)
	 */
	@Override
	public void delete(String cloudId) {
		if (StringUtils.isEmpty(cloudId)) {
			throw new IllegalArgumentException(
					"the cloudId param can not be empty");
		}
		this.getCloudFunctionRepository().delete(cloudId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.cloudfunc.CloudFunctionService#
	 * enable(java.lang.String, boolean)
	 */
	@Override
	public void enable(String cloudId, boolean enabled) {
		CloudFunction entity = (CloudFunction) getCloudFunctionRepository().findOne(cloudId);
		if (null == entity) {
			throw new SystemModuleException(
					"can not found the cloudfunction object of id:" + cloudId);
		}

		entity.setEnabled(enabled);
		getCloudFunctionRepository().save(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.cloudfunc.CloudFunctionService#
	 * removeLink(java.lang.String, java.lang.String)
	 */
	@Override
	public void removeLink(String cloudId, String appId) {
		// TODO could remove directly by custom method
		Page<AppCloudMappingEntry> page = getAppCloudMappingRepository()
				.findByQuery(
						Query.query(Criteria.where("cloudId").is(cloudId)
								.and("appId").is(appId)));
		if (!page.getContent().isEmpty()) {
			getAppCloudMappingRepository().delete(
					page.getContent().iterator().next());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.cloudfunc.CloudFunctionService#
	 * findAll()
	 */
	@Override
	public List<CloudFunctionVo> findAll() {
		List<CloudFunction> list = this.getCloudFunctionRepository().findAll(
				new Sort("order"));
		return BeanMapper.mapList(list, CloudFunctionVo.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.foreveross.bsl.system.application.CloudFunctionService#getById(java
	 * .lang.String)
	 */
	@Override
	public CloudFunctionVo getById(String id) {
		CloudFunction cf = new CloudFunction();
		cf = (CloudFunction) cf.findOne(id);
		if (cf == null)
			throw new SystemModuleException("找不到云服务:" + id);
		return BeanMapper.map(cf, CloudFunctionVo.class);
	}

}
